package com.ruoyi.kong.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.kong.entity.KongEntity;
import com.ruoyi.kong.entity.KongTarget;
import com.ruoyi.kong.entity.KongUpstream;
import com.ruoyi.kong.entity.KongUpstreamVo;
import com.ruoyi.kong.service.IKongUpstreamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.annotation.Target;

@RestController
@RequestMapping("/kong/upstream")
public class KongUpstreamController extends BaseController {

    @Autowired
    private IKongUpstreamService kongUpstreamService;

    @GetMapping("/list")
    public TableDataInfo getUpstreams(){
        startPage();
        KongEntity<KongUpstream> upstreams = kongUpstreamService.upstreams();
        return getDataTable(upstreams.getData());
    }

    @PostMapping("/add")
    public AjaxResult addUpstream(@RequestBody KongUpstreamVo kongUpstream){
        logger.info(kongUpstream.toString());
        KongUpstream _kongUpstream = kongUpstreamService.addUpstream(kongUpstream);
        return AjaxResult.success(_kongUpstream);
    }


    @PostMapping("/update/{id}")
    public AjaxResult updateUpstream(@PathVariable("id") String id,@RequestBody KongUpstreamVo kongUpstream){
        KongUpstream upstream = kongUpstreamService.updateUpstream(id, kongUpstream);
        return AjaxResult.success(upstream);
    }


    @PostMapping("/delete/{id}")
    public AjaxResult deleteUpstream(@PathVariable("id") String id){
        kongUpstreamService.deleteUpstream(id);
        return success();
    }

    @GetMapping("/get/{id}")
    public AjaxResult getUpstream(@PathVariable("id") String id){
        KongUpstream kongUpstream = kongUpstreamService.upstreamsById(id);
        return AjaxResult.success(kongUpstream);
    }

    @GetMapping("/target/{id}")
    public TableDataInfo getTargetsByUpstreamId(@PathVariable("id") String id){
        startPage();
        KongEntity<KongTarget> kongTargetKongEntity = kongUpstreamService.targetsByUpstreamId(id);
        return  getDataTable(kongTargetKongEntity.getData());
    }

    @PostMapping("/target/add/{upstreamId}")
    public AjaxResult addTarget(@PathVariable("upstreamId") String upstreamId, @RequestBody KongTarget target){
        KongTarget kongTarget = kongUpstreamService.addTarget(upstreamId, target);
        return  AjaxResult.success(kongTarget);
    }

    @PostMapping("/target/delete/{upstreamId}/{id}")
    public AjaxResult deleteTarget(@PathVariable("upstreamId") String upstreamId,@PathVariable("id") String id){
        KongTarget kongTarget = kongUpstreamService.deleteTarget(upstreamId,id);
        return  AjaxResult.success(kongTarget);
    }

}
